/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.controller.contentType;

import java.util.Iterator;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.servlet.http.HttpServletRequest;
import offset.nodes.server.config.NodesConfig;

/**
 *
 * @author Walter L�tgenau
 */
public class ContentTypeDispatcher {
    static ContentTypeDispatcher dispatcher = new ContentTypeDispatcher();

    /**
     * Implements the singleton pattern.
     *
     * @return The content type dispatcher
     */
    public static ContentTypeDispatcher getInstance() {
        return dispatcher;
    }

    /**
     * Retrieve the content type for this node, consider request parameters as well.
     *
     * @param node The node, for which the content type shall be determined
     * @param request The servlet request
     * @return The content type
     */
    public String getContentType(Node node, HttpServletRequest request) throws RepositoryException {
        Iterator<ContentTypeMapper> i = NodesConfig.getInstance().getContentTypeMapperConfig().getContentTypeMappers().iterator();
        while (i.hasNext()) {
            ContentTypeMapper mapper = i.next();
            String contentType = mapper.getContentType(node, request);
            if (contentType != null)
                return contentType;
        }

        return  null;
    }
}
